
import { type PaginationProps } from "@pureadmin/table";
import { MonitoringStrategyAccessBaseData, MonitoringStrategyAccess, MonitoringStrategyAccessList, WarningInformationList } from "@/api/stu/bankBigData";
import { useRoute } from "vue-router";
import { ElMessage, FormInstance } from "element-plus";
import { ref, reactive, onMounted } from "vue";
export function useCommon() {
  const route = useRoute();
  const dataList = ref<any>([]);
  const dataList1 = ref<any>([]);
  const dataList2 = ref<any>([]);
  const loading = ref<boolean>(false);
  const newShareVisible = ref<boolean>(false);
  const detailVisible = ref<boolean>(false);
  const tLoding = ref<boolean>(false);
  const accessSelectData = ref()
  const BtnLoading = ref(false)
  const dataForm = reactive({
    id: ''
  })
  const progress = ref(0)
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true,
    pageSizes: [5, 10, 15],
  });
  const pagination1 = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true,
    pageSizes: [5, 10, 15],
  });
  const pagination2 = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true,
    pageSizes: [5, 10, 15],
  });
  const columns: TableColumnList = [
    {
      label: "序号",
      type: "index",
      width: 120
    },
    {
      label: "模型名称",
      prop: "policyName",
      minWidth: 200
    },
    {
      label: "客户类型",
      prop: "customerType",
      minWidth: 200
    },
    {
      label: "操作",
      fixed: "right",
      width: 180,
      slot: "operation"
    }
  ];
  const columns1: TableColumnList = [
    {
      label: "序号",
      type: "index",
      width: 120
    },
    {
      label: "客户名称",
      prop: "customerName",
      minWidth: 200
    },
    {
      label: "所属行业",
      prop: "industry",
      minWidth: 200
    },
    {
      label: "预警信息",
      prop: "warningInfo",
      minWidth: 200
    },
    {
      label: "处理结果",
      prop: "handleResult",
      minWidth: 200
    },
  ];
  const columns2: TableColumnList = [
    {
      label: "序号",
      type: "index",
      minWidth: 100
    },
    {
      label: "客户名称",
      prop: "customerName",
      minWidth: 200
    },
    {
      label: "所属行业",
      prop: "careerBackground",
      minWidth: 200
    },
    {
      label: "预警信息",
      prop: "warningInfo",
      minWidth: 200
    },
    {
      label: "处理结果",
      prop: "handleResult",
      minWidth: 200
    },
  ];

  const newShareFrom = reactive({
    shareTitle: "",
    apiInfoDataShareId: [],
    shareType: "",
    safety: "",
    password: "",
    expiration: "",
    isInternalMerge: ""
  })



  //获取列表数据
  async function getList() {
    const respone = await MonitoringStrategyAccessList({
      pageIndex: pagination.currentPage,
      pageCount: pagination.pageSize,
      taskId: route.query.taskId,
      planId: route.query.planId,
    });
    if (respone.success) {
      respone.data?.items.forEach(item => {
        item.createTime = item.createTime.substring(0, 10)
      })
      pagination.total = respone.data.totalCount;
      dataList.value = respone.data.items;
    } else {
      ElMessage.error(respone.msg);
    }
  }
  async function getList1() {
    const data = {
      pageIndex: pagination1.currentPage,
      pageCount: pagination1.pageSize,
      taskId: route.query.taskId,
      planId: route.query.planId,
      detailId: dataForm.id,
      type: 1,
    }
    let res = await WarningInformationList(data)
    if (res.success) {
      pagination1.total = res.data.totalCount;
      dataList1.value = res.data.items;
    } else {
      ElMessage.error(res.msg);
    }
  }
  async function getList2() {
    const data = {
      pageIndex: pagination2.currentPage,
      pageCount: pagination2.pageSize,
      taskId: route.query.taskId,
      planId: route.query.planId,
      detailId: dataForm.id,
      type: 2,
    }
    let res = await WarningInformationList(data)
    if (res.success) {
      pagination2.total = res.data.totalCount;
      dataList2.value = res.data.items;
    } else {
      ElMessage.error(res.msg);
    }
  }


  function OUTBtnLoding() {
    setTimeout(() => {
      BtnLoading.value = false
    }, 1000)
  }
  async function addTask() {
    if (dataForm.id == '' || !dataForm.id) {
      ElMessage.warning('请先选择任务')
      return
    }
    const detailId = dataForm.id
    const data = {
      taskId: route.query.taskId,
      planId: route.query.planId,
      detailId: dataForm.id
    }
    BtnLoading.value = true
    OUTBtnLoding()
    let res = await MonitoringStrategyAccess(data)
    if (res.status == 200) {
      newShareVisible.value = false
      tLoding.value = true
      getList()
      const interval = setInterval(() => {
        if (progress.value < 100) {
          progress.value += 20;
        } else {
          tLoding.value = false
          ElMessage.success(res.msg);
          getDetailList(detailId)
          progress.value = 0
          clearInterval(interval);
        }
      }, 1000);
    } else {
      ElMessage.error(res.msg);
    }
  }

  async function getDetailList(id) {
    dataForm.id = id
    getList1()
    getList2()
    detailVisible.value = true
  }
  function handleSizeChange(val) {
    getList()
  }
  function handleCurrentChange(val) {
    // GetMyDataSystem()
    getList()
  }
  function handleSizeChange1(val) {
    getList1()
  }
  function handleCurrentChange1(val) {
    // GetMyDataSystem()
    getList1()
  }
  function handleSizeChange2(val) {
    getList2()
  }
  function handleCurrentChange2(val) {
    // GetMyDataSystem()
    getList2()
  }
  onMounted(async () => {
    const data = {
      taskId: route.query.taskId,
      planId: route.query.planId,
    }
    let res = await MonitoringStrategyAccessBaseData(data)
    if (res.success) {
      accessSelectData.value = res?.data
    } else {
      ElMessage.error(res.msg);
    }
    getList()
  })

  function cancelnewShareVisible() {
    newShareVisible.value = false
    dataForm.id = undefined
  }

  return {
    dataList,
    loading,
    pagination,
    columns,
    newShareFrom,
    handleSizeChange,
    handleCurrentChange,
    accessSelectData,
    dataForm,
    newShareVisible,
    addTask,
    tLoding,
    getDetailList,
    detailVisible,
    dataList1,
    columns1,
    pagination1,
    handleSizeChange1,
    handleCurrentChange1,
    dataList2,
    columns2,
    pagination2,
    handleSizeChange2,
    handleCurrentChange2,
    progress,
    cancelnewShareVisible,
  }
}
